package carassius.GUI.Calendar;

import carassius.BLL.GregorianCalendarWithEvents;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.GregorianCalendar;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import javax.swing.JButton;

/**
 *
 * @author siebz0r
 */
public class MonthButton
		extends JButton
		implements PropertyChangeListener
{
	//<editor-fold defaultstate="collapsed" desc="Fields">
	private GregorianCalendarWithEvents _calendar;
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Constructors">
	public MonthButton()
	{
		this(new GregorianCalendarWithEvents());
	}

	public MonthButton(int year, int month)
	{
		this(new GregorianCalendarWithEvents(year, month, new GregorianCalendar().get(GregorianCalendar.DATE)));
	}

	public MonthButton(GregorianCalendarWithEvents date)
	{
		_calendar = date;
		try
		{
			setText(ResourceBundle.getBundle("carassius.GUI.Calendar.resources.month").getString(Integer.toString(_calendar.get(GregorianCalendar.MONTH))));
		}
		catch (MissingResourceException ex)
		{
			setText("");
		}
		_calendar.addPropertyChangeListener("month", this);
	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Public Properties">
	public int getYear()
	{
		return _calendar.get(GregorianCalendar.YEAR);
	}

	public int getMonth()
	{
		return _calendar.get(GregorianCalendar.MONTH);
	}

	public void setYear(int year)
	{
		_calendar.set(GregorianCalendar.YEAR, year);
	}

	public void setMonth(int month)
	{
		_calendar.set(GregorianCalendar.MONTH, month);
		try
		{
			setText(ResourceBundle.getBundle("carassius.GUI.Calendar.resources.month").getString(Integer.toString(month)));
		}
		catch (MissingResourceException ex)
		{
			setText("");
		}

	}
	//</editor-fold>

	//<editor-fold defaultstate="collapsed" desc="Public Methods">
	public void addMonth()
	{
		_calendar.add(GregorianCalendar.MONTH, 1);
	}

	public void subtractMonth()
	{
		_calendar.add(GregorianCalendar.MONTH, -1);
	}
	//</editor-fold>

	@Override
	public void propertyChange(PropertyChangeEvent evt)
	{
		if ("month".equals(evt.getPropertyName()))
		{
			setMonth((Integer) evt.getNewValue());
		}
	}
}
